//
//  CharacterMaleView.m
//  us
//
//  Created by lhx on 2025/4/1.
//

#import "CharacterView.h"

@interface CharacterView ()

@property (nonatomic, strong) NSMutableArray <UIImageView *>*imageViewsList;

@end

@implementation CharacterView

-  (void)awakeFromNib{
    [super awakeFromNib];
    [self initialization];
    
    if (!_character) {
        self.character = [USCartoonCharacter defaultBoyCharacter];
    }
    
}

- (instancetype)initWithFrame:(CGRect)frame
                    character:(USCartoonCharacter *)character{
    if (self = [super initWithFrame:frame]) {
        [self initialization];
        self.character = character;
    }
    return self;
}

- (void)setIsSelect:(BOOL)isSelect {
    _isSelect = isSelect;
    self.alpha = isSelect ? 1 : 0.6;
}

- (void) changePart:(CharacterUnitModel *)part {
    [self applyPartModel:part];
    
    for (CharacterUnitModel *model in self.character.parts) {
        if (model.type == part.type) {
            model.icon = part.icon;
            break;
        }
    }
}

- (void)setCharacter:(USCartoonCharacter *)character {
    _character = character;
    for (CharacterUnitModel *part in character.parts) {
        [self applyPartModel:part];
    }
}

- (void)applyPartModel:(CharacterUnitModel *)part {
    if (part.type < self.imageViewsList.count) {
        UIImageView *imgView = self.imageViewsList[part.type];
        imgView.hidden = [part.icon isEqualToString:@""] ? YES : NO;
        if (part.icon.length > 0) {
            imgView.image = [UIImage imageNamed:part.icon];
            imgView.hidden = [part.icon isEqualToString:@"character_empty"] ? YES : NO;
        }
       
    }
}


- (void)initialization {
    //独立的脸部
    UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cheek"]];
    [self addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.backgroundColor = [UIColor clearColor];
    for (int i = 0; i<6; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        [self addSubview:imgView];
        [self.imageViewsList addObject:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }

}

- (NSMutableArray <UIImageView *>*)imageViewsList {
    if(_imageViewsList == nil) {
        _imageViewsList = [NSMutableArray array];
    }
    return _imageViewsList;
}

+ (UIImage *)imageForCharacter:(USCartoonCharacter *)character size:(CGSize)size {
    if (!character || character.parts.count == 0) return nil;
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (!context) return nil;
    
    NSMutableArray <NSString *>*imgNames = [NSMutableArray array];
    [imgNames addObject:@"cheek"];
    // 根据 type 排序，确保图层顺序一致（比如头发在最上）
    NSArray<CharacterUnitModel *> *sortedParts = [character.parts sortedArrayUsingComparator:^NSComparisonResult(CharacterUnitModel *obj1, CharacterUnitModel *obj2) {
        return obj1.type - obj2.type;
    }];
    
    for (CharacterUnitModel *part in sortedParts) {
        if (part.icon.length > 0 && ![part.icon isEqualToString:@"character_empty"]) {
            [imgNames addObject:part.icon];
        }
    }
    
    [imgNames enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIImage *img = [UIImage imageNamed:obj];
        if (!img) return;
        // 居中绘制
        CGSize imgSize = img.size;
        CGFloat x = (size.width - imgSize.width) / 2.0;
        CGFloat y = (size.height - imgSize.height) / 2.0;
        CGRect drawRect = CGRectMake(x, y, imgSize.width, imgSize.height);
        [img drawInRect:drawRect];
        
    }];
    
    
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return finalImage;
}

+ (CGFloat)offsetXForCharacterCloserWithWidth:(CGFloat)width{
    
    CGFloat x = 10/85.f * width;
    return x;

}



@end
